diff options
author | Maxence Lange <maxence@artificial-owl.com> | 2018-07-27 13:07:10 +0300 |
---|---|---|
committer | Maxence Lange <maxence@artificial-owl.com> | 2018-07-27 13:07:10 +0300 |
commit | 665530336392412487b1dc74155bf2de29f7e979 (patch) | |
tree | de35aca8ae18cb8c84f267d63265ad1eb7ce363d /lib | |
parent | be05d95051135ab86860047bfe9c750b2216a902 (diff) |
more tests
Signed-off-by: Maxence Lange <maxence@artificial-owl.com>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/Command/Test.php | 456 | ||||
-rw-r--r-- | lib/Provider/TestProvider.php | 8 | ||||
-rw-r--r-- | lib/Service/TestService.php | 44 |
3 files changed, 360 insertions, 148 deletions
diff --git a/lib/Command/Test.php b/lib/Command/Test.php index 82687b0..59bd6be 100644 --- a/lib/Command/Test.php +++ b/lib/Command/Test.php @@ -28,18 +28,24 @@ namespace OCA\FullTextSearch\Command; use Exception; use OCA\FullTextSearch\Exceptions\InterruptException; +use OCA\FullTextSearch\Exceptions\ProviderDoesNotExistException; use OCA\FullTextSearch\Exceptions\ProviderIsNotCompatibleException; +use OCA\FullTextSearch\Exceptions\ProviderIsNotUniqueException; use OCA\FullTextSearch\Exceptions\RunnerAlreadyUpException; use OCA\FullTextSearch\Exceptions\TickDoesNotExistException; use OCA\FullTextSearch\IFullTextSearchPlatform; use OCA\FullTextSearch\IFullTextSearchProvider; +use OCA\FullTextSearch\Model\DocumentAccess; use OCA\FullTextSearch\Model\ExtendedBase; use OCA\FullTextSearch\Model\IndexOptions; use OCA\FullTextSearch\Model\Runner; +use OCA\FullTextSearch\Model\SearchRequest; +use OCA\FullTextSearch\Model\SearchResult; use OCA\FullTextSearch\Provider\TestProvider; use OCA\FullTextSearch\Service\IndexService; use OCA\FullTextSearch\Service\MiscService; use OCA\FullTextSearch\Service\PlatformService; +use OCA\FullTextSearch\Service\ProviderService; use OCA\FullTextSearch\Service\RunningService; use OCA\FullTextSearch\Service\TestService; use OCP\AppFramework\QueryException; @@ -50,15 +56,20 @@ use Symfony\Component\Console\Output\OutputInterface; class Test extends ExtendedBase { + const DELAY_STABILIZE_PLATFORM = 3; + /** @var RunningService */ private $runningService; - /** @var IndexService */ - private $indexService; - /** @var PlatformService */ private $platformService; + /** @var ProviderService */ + private $providerService; + + /** @var IndexService */ + private $indexService; + /** @var TestService */ private $testService; @@ -77,20 +88,23 @@ class Test extends ExtendedBase { * Index constructor. * * @param RunningService $runningService + * @param ProviderService $providerService * @param IndexService $indexService * @param PlatformService $platformService * @param TestService $testService * @param MiscService $miscService */ public function __construct( - RunningService $runningService, IndexService $indexService, - PlatformService $platformService, TestService $testService, MiscService $miscService + RunningService $runningService, PlatformService $platformService, + ProviderService $providerService, IndexService $indexService, TestService $testService, + MiscService $miscService ) { parent::__construct(); - $this->indexService = $indexService; $this->runningService = $runningService; $this->platformService = $platformService; + $this->providerService = $providerService; + $this->indexService = $indexService; $this->testService = $testService; $this->miscService = $miscService; } @@ -103,7 +117,11 @@ class Test extends ExtendedBase { parent::configure(); $this->setName('fulltextsearch:test') ->setDescription('Testing the platform setup') - ->addOption('json', 'j', InputOption::VALUE_NONE, 'return result as JSON'); + ->addOption('json', 'j', InputOption::VALUE_NONE, 'return result as JSON') + ->addOption( + 'platform_delay', 'd', InputOption::VALUE_REQUIRED, + 'change DELAY_STABILIZE_PLATFORM' + ); } @@ -116,26 +134,39 @@ class Test extends ExtendedBase { */ protected function execute(InputInterface $input, OutputInterface $output) { $this->isJson = ($input->getOption('json') === true); + $platformDelay = ($input->getOption('platform_delay') > 0) ? $input->getOption( + 'platform_delay' + ) : self::DELAY_STABILIZE_PLATFORM; + echo '$' . $input->getOption('platform_delay') . '$'; $this->output($output, '.Testing your current setup:'); try { $testProvider = $this->testCreatingProvider($output); $this->testMockedProvider($output, $testProvider); $testPlatform = $this->testLoadingPlatform($output); + $this->testLockingProcess($output, $testPlatform, $testProvider); } catch (Exception $e) { $this->output($output, false); throw $e; } try { - $this->testLockingProcess($output, $testPlatform, $testProvider); + $this->testResetTest($output, $testProvider); + $this->pause($output, $platformDelay); $this->testIndexingDocuments($output, $testPlatform, $testProvider); + $this->pause($output, $platformDelay); $this->testContentLicense($output, $testPlatform); + $this->testSearchSimple($output, $testPlatform, $testProvider); + + $this->testUpdatingDocumentsAccess($output, $testPlatform, $testProvider); + $this->pause($output, $platformDelay); + $this->testSearchAccess($output, $testPlatform, $testProvider); + $this->testSearchShare($output, $testPlatform, $testProvider); + $this->testResetTest($output, $testProvider); $this->testUnlockingProcess($output); } catch (Exception $e) { - $this->output($output, false); $this->output($output, 'Error detected, unlocking process'); $this->runner->stop(); @@ -144,52 +175,21 @@ class Test extends ExtendedBase { throw $e; } - -// $options = $this->generateIndexOptions($input); -// -// try { -// $this->runner->sourceIsCommandLine($this, $output); -// $this->runner->start(); -// -// $providers = $this->providerService->getProviders(); -// foreach ($providers as $provider) { -// -// if (!$this->isIncludedProvider($options, $provider->getId())) { -// continue; -// } -// -// $this->runner->output('indexing ' . $provider->getName() . '.'); -// $provider->setRunner($this->runner); -// $provider->setIndexOptions($options); -// $this->indexProvider($provider, $options); -// } -// -// } catch (Exception $e) { -// $this->runner->exception($e->getMessage(), true); -// throw $e; -// } -// -// $this->runner->stop(); - $this->output($output, '', true); } /** * @return IFullTextSearchProvider - * @throws QueryException * @throws ProviderIsNotCompatibleException + * @throws QueryException + * @throws ProviderDoesNotExistException + * @throws ProviderIsNotUniqueException */ private function generateMockProvider() { - $providerId = 'OCA\FullTextSearch\Provider\TestProvider'; - $provider = \OC::$server->query((string)$providerId); - if (!($provider instanceof IFullTextSearchProvider)) { - throw new ProviderIsNotCompatibleException( - 'TestProvider is not a compatible IFullTextSearchProvider' - ); - } + $this->providerService->loadProvider('OCA\FullTextSearch\Provider\TestProvider'); - return $provider; + return $this->providerService->getProvider(TestProvider::TEST_PROVIDER_ID); } @@ -232,7 +232,9 @@ class Test extends ExtendedBase { * @param $output * * @return IFullTextSearchProvider + * @throws ProviderDoesNotExistException * @throws ProviderIsNotCompatibleException + * @throws ProviderIsNotUniqueException * @throws QueryException */ private function testCreatingProvider($output) { @@ -250,7 +252,8 @@ class Test extends ExtendedBase { */ private function testMockedProvider($output, IFullTextSearchProvider $testProvider) { $this->output($output, 'Testing mocked provider: get indexable documents.'); - $indexableDocuments = $testProvider->generateIndexableDocuments('user'); + $indexableDocuments = + $testProvider->generateIndexableDocuments(TestService::DOCUMENT_USER1); $this->output($output, '(' . sizeof($indexableDocuments) . ' items)', false); $this->output($output, true); } @@ -269,7 +272,9 @@ class Test extends ExtendedBase { $this->output($output, true); $this->output($output, 'Testing search platform.'); - $testPlatform->testPlatform(); + $this->output( + $output, (($testPlatform->testPlatform()) ? 'found index' : 'index not found'), false + ); $this->output($output, true); return $testPlatform; @@ -299,6 +304,20 @@ class Test extends ExtendedBase { /** * @param OutputInterface $output + * @param IFullTextSearchProvider $testProvider + * + * @throws Exception + */ + private function testResetTest(OutputInterface $output, IFullTextSearchProvider $testProvider + ) { + $this->output($output, 'Removing test.'); + $this->indexService->resetIndex($testProvider->getId()); + $this->output($output, true); + } + + + /** + * @param OutputInterface $output * @param IFullTextSearchPlatform $testPlatform * @param IFullTextSearchProvider $testProvider * @@ -316,7 +335,7 @@ class Test extends ExtendedBase { ] ); $this->indexService->indexProviderContentFromUser( - $testPlatform, $testProvider, 'user', $options + $testPlatform, $testProvider, TestService::DOCUMENT_USER1, $options ); $this->output($output, true); } @@ -335,17 +354,16 @@ class Test extends ExtendedBase { try { $this->output($output, 'Retreiving content from a big index (license).'); $indexDocument = $testPlatform->getDocument( - TestProvider::TEST_PROVIDER_ID, TestService::DOCUMENT_TEST_LICENSE + TestProvider::TEST_PROVIDER_ID, TestService::DOCUMENT_TYPE_LICENSE ); $this->output( $output, '(size: ' . $indexDocument->getContentSize() . ')', false ); $this->output($output, true); - } catch (Exception $e) { throw new Exception( - "Issue while getting test document '" . TestService::DOCUMENT_TEST_LICENSE + "Issue while getting test document '" . TestService::DOCUMENT_TYPE_LICENSE . "' from search platform: " . $e->getMessage() ); } @@ -360,6 +378,137 @@ class Test extends ExtendedBase { /** * @param OutputInterface $output + * @param IFullTextSearchPlatform $testPlatform + * + * @param IFullTextSearchProvider $testProvider + * + * @throws Exception + */ + private function testSearchSimple( + OutputInterface $output, IFullTextSearchPlatform $testPlatform, + IFullTextSearchProvider $testProvider + ) { + + $this->output($output, 'Searching basic keywords:'); + + $access = new DocumentAccess(); + $access->setViewerId(TestService::DOCUMENT_USER1); + + $this->search( + $output, $testPlatform, $testProvider, $access, 'test', + [TestService::DOCUMENT_TYPE_SIMPLE] + ); + $this->search( + $output, $testPlatform, $testProvider, $access, 'this is test', + [TestService::DOCUMENT_TYPE_SIMPLE, TestService::DOCUMENT_TYPE_LICENSE] + ); + $this->search( + $output, $testPlatform, $testProvider, $access, '"this is test"', + [] + ); + $this->search( + $output, $testPlatform, $testProvider, $access, '"this is a test"', + [TestService::DOCUMENT_TYPE_SIMPLE] + ); + $this->search( + $output, $testPlatform, $testProvider, $access, 'this is -test', + [TestService::DOCUMENT_TYPE_LICENSE] + ); + $this->search( + $output, $testPlatform, $testProvider, $access, 'this is +test', + [TestService::DOCUMENT_TYPE_SIMPLE] + ); + $this->search( + $output, $testPlatform, $testProvider, $access, '-this is test', + [] + ); + } + + + /** + * @param OutputInterface $output + * @param IFullTextSearchPlatform $testPlatform + * @param IFullTextSearchProvider $testProvider + * + * @throws InterruptException + * @throws TickDoesNotExistException + */ + private function testUpdatingDocumentsAccess( + OutputInterface $output, IFullTextSearchPlatform $testPlatform, + IFullTextSearchProvider $testProvider + ) { + $this->output($output, 'Updating documents access.'); + $options = new IndexOptions( + [ + 'provider' => TestProvider::TEST_PROVIDER_ID, + TestService::DOCUMENT_INDEXING_OPTION => TestService::DOCUMENT_INDEXING_ACCESS + ] + ); + $testProvider->setIndexOptions($options); + $this->indexService->indexProviderContentFromUser( + $testPlatform, $testProvider, TestService::DOCUMENT_USER1, $options + ); + $this->output($output, true); + } + + + /** + * @param OutputInterface $output + * @param IFullTextSearchPlatform $platform + * + * @param IFullTextSearchProvider $provider + * + * @throws Exception + */ + private function testSearchAccess( + OutputInterface $output, IFullTextSearchPlatform $platform, + IFullTextSearchProvider $provider + ) { + + $this->output($output, 'Searching with group access rights:'); + + $this->searchGroups($output, $platform, $provider, [], []); + $this->searchGroups( + $output, $platform, $provider, [TestService::DOCUMENT_GROUP1], + [TestService::DOCUMENT_TYPE_LICENSE] + ); + $this->searchGroups( + $output, $platform, $provider, + [TestService::DOCUMENT_GROUP1, TestService::DOCUMENT_GROUP2], + [TestService::DOCUMENT_TYPE_LICENSE] + ); + $this->searchGroups( + $output, $platform, $provider, + [TestService::DOCUMENT_NOTGROUP, TestService::DOCUMENT_GROUP2], + [TestService::DOCUMENT_TYPE_LICENSE] + ); + $this->searchGroups($output, $platform, $provider, [TestService::DOCUMENT_NOTGROUP], []); + } + + + /** + * @param OutputInterface $output + * @param IFullTextSearchPlatform $platform + * + * @param IFullTextSearchProvider $provider + * + * @throws Exception + */ + private function testSearchShare( + OutputInterface $output, IFullTextSearchPlatform $platform, + IFullTextSearchProvider $provider + ) { + + $this->output($output, 'Searching with share rights:'); + + $this->searchUsers($output, $platform, $provider, TestService::DOCUMENT_NOTUSER, []); + $this->searchUsers($output, $platform, $provider, TestService::DOCUMENT_USER2, ['license']); + $this->searchUsers($output, $platform, $provider, TestService::DOCUMENT_USER3, ['license']); + } + + + /** + * @param OutputInterface $output * * @throws TickDoesNotExistException */ @@ -370,92 +519,127 @@ class Test extends ExtendedBase { } + /** + * @param OutputInterface $output + * @param IFullTextSearchPlatform $testPlatform + * @param IFullTextSearchProvider $testProvider + * @param DocumentAccess $access + * @param string $search + * @param array $expected + * @param string $moreOutput + * + * @throws Exception + */ + private function search( + OutputInterface $output, IFullTextSearchPlatform $testPlatform, + IFullTextSearchProvider $testProvider, + DocumentAccess $access, $search, $expected, $moreOutput = '' + ) { + $this->output( + $output, + " - '" . $search . "'" . (($moreOutput === '') ? '' : ' - ' . $moreOutput . ' - ') + ); + $request = new SearchRequest(); + + $request->setSearch($search); + $searchResult = $testPlatform->searchDocuments($testProvider, $access, $request); + $this->output( + $output, + '(result: ' . $searchResult->getCount() . ', expected: ' . json_encode($expected) . ')', + false + ); + $this->compareSearchResult($searchResult, $expected); + $this->output($output, true); + } + + + /** + * @param OutputInterface $output + * @param IFullTextSearchPlatform $testPlatform + * @param IFullTextSearchProvider $testProvider + * @param array $groups + * @param array $expected + * + * @throws Exception + */ + private function searchGroups( + OutputInterface $output, IFullTextSearchPlatform $testPlatform, + IFullTextSearchProvider $testProvider, $groups, $expected + ) { + + $access = new DocumentAccess(); + $access->setViewerId(TestService::DOCUMENT_NOTUSER); + $access->setGroups($groups); + + $this->search( + $output, $testPlatform, $testProvider, $access, 'license', + $expected, json_encode($groups) + ); + } + + + /** + * @param OutputInterface $output + * @param IFullTextSearchPlatform $testPlatform + * @param IFullTextSearchProvider $testProvider + * @param string $user + * @param array $expected + * + * @throws Exception + */ + private function searchUsers( + OutputInterface $output, IFullTextSearchPlatform $testPlatform, + IFullTextSearchProvider $testProvider, $user, $expected + ) { + $access = new DocumentAccess(); + $access->setViewerId($user); + $this->search( + $output, $testPlatform, $testProvider, $access, 'license', + $expected, $user + ); + } + + + /** + * @param SearchResult $searchResult + * @param $entries + * + * @throws Exception + */ + private function compareSearchResult(SearchResult $searchResult, $entries) { + $documents = $searchResult->getDocuments(); + if (sizeof($documents) !== sizeof($entries)) { + throw new \Exception('Unexpected SearchResult: ' . json_encode($searchResult)); + } + + foreach ($documents as $document) { + if (!in_array($document->getId(), $entries)) { + throw new \Exception('Unexpected Document: ' . json_encode($document)); + } + } + } + + + /** + * @param OutputInterface $output + * @param int $s + * + * @throws InterruptException + */ + private function pause(OutputInterface $output, $s) { + $this->output($output, 'Pausing ' . $s . ' seconds'); + + for ($i = 1; $i <= $s; $i++) { + if (time_nanosleep(1, 0) !== true) { + throw new InterruptException('Interrupted by user'); + } + + $this->output($output, $i, false); + } + + $this->output($output, true); + } -// /** -// * @param IFullTextSearchProvider $provider -// * @param IndexOptions $options -// * -// * @throws Exception -// */ -// private function indexProvider(IFullTextSearchProvider $provider, IndexOptions $options) { -// $platform = $this->platformService->getPlatform(); -// $platform->initializeIndex(); -// $provider->onInitializingIndex($platform); -// -// $platform->setRunner($this->runner); -// -// $users = $this->generateUserList($options); -// foreach ($users as $user) { -// if ($user === null) { -// continue; -// } -// -// $this->runner->output(' USER: ' . $user->getUID()); -// $this->indexService->indexProviderContentFromUser( -// $platform, $provider, $user->getUID(), $options -// ); -// } -// -// $this->providerService->setProviderAsIndexed($provider, true); -// -// } -// -// -// /** -// * @param InputInterface $input -// * -// * @return IndexOptions -// */ -// private function generateIndexOptions(InputInterface $input) { -// $jsonOptions = $input->getArgument('options'); -// $options = json_decode($jsonOptions, true); -// -// if (!is_array($options)) { -// $options = []; -// } -// -// return new IndexOptions($options); -// } -// -// -// /** -// * @param IndexOptions $options -// * @param string $providerId -// * -// * @return bool -// */ -// private function isIncludedProvider(IndexOptions $options, $providerId) { -// if ($options->getOption('provider', '') !== '' -// && $options->getOption('provider') !== $providerId) { -// return false; -// } -// -// if ($options->getOption('providers', null) !== null -// && is_array($options->getOption('providers'))) { -// return (in_array($providerId, $options->getOption('providers'))); -// } -// -// return true; -// } -// -// -// /** -// * @param IndexOptions $options -// * -// * @return array -// */ -// private function generateUserList(IndexOptions $options) { -// if ($options->getOption('user', '') !== '') { -// return [$this->userManager->get($options->getOption('user'))]; -// } -// -// if ($options->getOption('users', null) !== null -// && is_array($options->getOption('users'))) { -// return array_map([$this->userManager, 'get'], $options->getOption('users')); -// } -// -// return $this->userManager->search(''); -// } } diff --git a/lib/Provider/TestProvider.php b/lib/Provider/TestProvider.php index 29808c8..d4a47a1 100644 --- a/lib/Provider/TestProvider.php +++ b/lib/Provider/TestProvider.php @@ -58,7 +58,7 @@ class TestProvider implements IFullTextSearchProvider { private $runner; /** @var IndexOptions */ - private $indexOptions = []; + private $indexOptions; /** @@ -74,6 +74,8 @@ class TestProvider implements IFullTextSearchProvider { $this->configService = $configService; $this->testService = $testService; $this->miscService = $miscService; + + $this->indexOptions = new IndexOptions(); } @@ -160,8 +162,8 @@ class TestProvider implements IFullTextSearchProvider { public function generateIndexableDocuments($userId) { $result = []; - $result[] = $this->testService->generateIndexDocumentContentLicense(); - $result[] = $this->testService->generateIndexDocumentSimple(); + $result[] = $this->testService->generateIndexDocumentContentLicense($this->indexOptions); + $result[] = $this->testService->generateIndexDocumentSimple($this->indexOptions); // $result[] = $this->testService->generateIndexDocuments(TestService::DOCUMENT_TEST_INDEX3); return $result; diff --git a/lib/Service/TestService.php b/lib/Service/TestService.php index 87a4b95..b11075d 100644 --- a/lib/Service/TestService.php +++ b/lib/Service/TestService.php @@ -30,6 +30,7 @@ use OCA\FullTextSearch\AppInfo\Application; use OCA\FullTextSearch\Exceptions\ProviderOptionsDoesNotExistException; use OCA\FullTextSearch\Model\DocumentAccess; use OCA\FullTextSearch\Model\IndexDocument; +use OCA\FullTextSearch\Model\IndexOptions; use OCA\FullTextSearch\Provider\TestProvider; use OCP\IConfig; use OCP\PreConditionNotMetException; @@ -37,11 +38,20 @@ use OCP\Util; class TestService { - const DOCUMENT_USER = 'user'; + const DOCUMENT_USER1 = 'user1'; + const DOCUMENT_USER2 = 'user2'; + const DOCUMENT_USER3 = 'user3'; + const DOCUMENT_NOTUSER = 'notuser'; - const DOCUMENT_TEST_LICENSE = 'license'; - const DOCUMENT_TEST_SIMPLE = 'simple'; - const DOCUMENT_TEST_ACCESS = 'access'; + const DOCUMENT_GROUP1 = 'group_1'; + const DOCUMENT_GROUP2 = 'group_2'; + const DOCUMENT_NOTGROUP = 'group_3'; + + const DOCUMENT_TYPE_LICENSE = 'license'; + const DOCUMENT_TYPE_SIMPLE = 'simple'; + + const DOCUMENT_INDEXING_OPTION = 'indexing'; + const DOCUMENT_INDEXING_ACCESS = 'access'; const LICENSE_HASH = '108322602bb857915803a84e23a2cc2f'; @@ -60,24 +70,40 @@ class TestService { /** + * @param IndexOptions $options + * * @return IndexDocument */ - public function generateIndexDocumentContentLicense() { - $indexDocument = $this->generateIndexDocument(self::DOCUMENT_TEST_LICENSE); + public function generateIndexDocumentContentLicense(IndexOptions $options = null) { + $indexDocument = $this->generateIndexDocument(self::DOCUMENT_TYPE_LICENSE); $content = file_get_contents(__DIR__ . '/../../LICENSE'); $indexDocument->setContent($content); + if ($options === null) { + return $indexDocument; + } + + if ($options->getOption(self::DOCUMENT_INDEXING_OPTION, '') + === self::DOCUMENT_INDEXING_ACCESS) { + $indexDocument->getAccess() + ->setGroups([self::DOCUMENT_GROUP1, self::DOCUMENT_GROUP2]); + $indexDocument->getAccess() + ->setUsers([self::DOCUMENT_USER2, self::DOCUMENT_USER3]); + } + return $indexDocument; } /** + * @param IndexOptions $options + * * @return IndexDocument */ - public function generateIndexDocumentSimple() { + public function generateIndexDocumentSimple(IndexOptions $options) { - $indexDocument = $this->generateIndexDocument(self::DOCUMENT_TEST_SIMPLE); + $indexDocument = $this->generateIndexDocument(self::DOCUMENT_TYPE_SIMPLE); $indexDocument->setContent('This is a test'); return $indexDocument; @@ -123,7 +149,7 @@ class TestService { private function generateIndexDocument($documentType) { $indexDocument = new IndexDocument(TestProvider::TEST_PROVIDER_ID, $documentType); - $access = new DocumentAccess(self::DOCUMENT_USER); + $access = new DocumentAccess(self::DOCUMENT_USER1); $indexDocument->setAccess($access); return $indexDocument; |