diff options
-rw-r--r-- | appinfo/register_command.php | 1 | ||||
-rw-r--r-- | lib/Command/Test.php | 231 |
2 files changed, 232 insertions, 0 deletions
diff --git a/appinfo/register_command.php b/appinfo/register_command.php index d1fa6d5..3037024 100644 --- a/appinfo/register_command.php +++ b/appinfo/register_command.php @@ -28,6 +28,7 @@ $app = new \OCA\Nextant\AppInfo\Application(); $c = $app->getContainer(); $application->add(new OCA\Nextant\Command\Check($c->query('ConfigService'), $c->query('SolrService'), $c->query('SolrToolsService'), $c->query('SolrAdminService'), $c->query('SettingsController'))); +$application->add(new OCA\Nextant\Command\Test($c->query('ConfigService'), $c->query('SolrService'), $c->query('SolrToolsService'), $c->query('SolrAdminService'), $c->query('IndexService'), $c->query('SettingsController'))); $application->add(new OCA\Nextant\Command\Clear($c->query('ConfigService'), $c->query('SolrService'), $c->query('SolrAdminService'))); $application->add(new OCA\Nextant\Command\Index(OC::$server->getUserManager(), $c->query('RootFolder'), $c->query('IndexService'), $c->query('QueueService'), $c->query('SolrService'), $c->query('SolrToolsService'), $c->query('SolrAdminService'), $c->query('ConfigService'), $c->query('SourceService'), $c->query('MiscService'))); $application->add(new OCA\Nextant\Command\Optimize($c->query('ConfigService'), $c->query('SolrService'), $c->query('SolrToolsService'))); diff --git a/lib/Command/Test.php b/lib/Command/Test.php new file mode 100644 index 0000000..a2ca3cf --- /dev/null +++ b/lib/Command/Test.php @@ -0,0 +1,231 @@ +<?php + +/** + * Nextcloud - nextant + * + * This file is licensed under the Affero General Public License version 3 or + * later. See the COPYING file. + * + * @author Maxence Lange <maxence@pontapreta.net> + * @copyright Maxence Lange 2017 + * @license GNU AGPL version 3 or any later version + * + * This program 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 (at your option) 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/>. + * + */ +namespace OCA\Nextant\Command; + +use OC\Core\Command\Base; +use \OCA\Nextant\Service\TestService; +use \OCA\Nextant\Items\ItemDocument; +use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; +use Symfony\Component\Console\Output\OutputInterface; + +class Test extends Base +{ + + private $configService; + + private $solrService; + + private $solrTools; + + private $solrAdmin; + + private $settingsController; + + public function __construct($configService, $solrService, $solrTools, $solrAdmin, $indexService, $settingsController) + { + parent::__construct(); + $this->configService = $configService; + $this->solrService = $solrService; + $this->solrTools = $solrTools; + $this->solrAdmin = $solrAdmin; + $this->indexService = $indexService; + $this->settingsController = $settingsController; + } + + protected function configure() + { + parent::configure(); + $this->setName('nextant:test')->setDescription('test your Nextant configuration'); + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + + // test ping + if (! $this->test_ping($output)) + return; + + // test schema + if (! $this->test_schema($output)) + return; + + // test extract + if (! $this->test_extract($output)) + return; + + // test update + if (! $this->test_update($output)) + return; + + // test search + if (! $this->test_search($output)) + return; + + // test delete + if (! $this->test_delete($output)) + return; + + $output->writeln(''); + $output->writeln('All test results were fine'); + } + + private function test_ping($output) + { + $output->write(' - Pinging Solr: '); + if (! $this->solrAdmin->ping($ierror)) { + $output->writeln('<error>fail</error> - ' . $ierror->getCode()); + return false; + } + + $output->writeln('<info>ok</info>'); + return true; + } + + private function test_schema($output) + { + $output->write(' - Checking Solr schema: '); + if (! $this->solrAdmin->checkSchema($ierror)) { + $output->writeln('<error>fail</error> - ' . $ierror->getCode()); + return false; + } + + $output->writeln('<info>ok</info>'); + return true; + } + + private function test_extract($output) + { + $output->write(' - Extracting test document: '); + $doc = TestService::generateTestDocument(1, '_nextant_test', __DIR__ . '/../../LICENSE', '/LICENSE'); + $data = array( + $doc + ); + $solrDocs = null; + $this->indexService->extract(ItemDocument::TYPE_TEST, '_nextant_test', $data, $solrDocs, true, $ierror); + + if (! $doc->isProcessed()) { + $output->writeln('<error>fail</error> - ' . $ierror->getCode()); + return false; + } + + $output->writeln('<info>ok</info>'); + return true; + } + + private function test_update($output) + { + $output->write(' - Updating test document: '); + $doc = TestService::generateTestDocument(1, '_nextant_test', __DIR__ . '/../../LICENSE', '/LICENSE2'); + $asource = $this->indexService->getDocuments(ItemDocument::TYPE_TEST, '_nextant_test', 1, $ierror); + + if ($asource === false || sizeof($asource) != 1 || (! key_exists('test_1', $asource))) { + $output->writeln('<error>fail</error> - ' . $ierror->getCode()); + return false; + } + + $source = $asource['test_1']; + + $doc->setPath('/LICENSE2'); + $doc->setShare(array( + 'nextant_test_share' + )); + $doc->setShareGroup(array( + 'nextant_test_share_group' + )); + $doc->deleted(false); + + $data = array( + $doc + ); + $this->indexService->updateDocuments(ItemDocument::TYPE_TEST, '_nextant_test', $data, $asource, $ierror); + + if (! $this->solrTools->commit(false, $ierror)) { + $output->writeln('<error>fail</error> - ' . $ierror->getCode()); + return false; + } + + if (! $source->isUpdated()) { + $output->writeln('<error>fail</error> - ' . $ierror->getCode()); + return false; + } + + $output->writeln('<info>ok</info>'); + return true; + } + + private function test_search($output) + { + $output->write(' - Searching test document: '); + + $keyword = 'LICENSE'; + $this->solrService->setOwner('_nextant_test'); + if ($result = $this->solrService->search($keyword, array(), $ierror)) { + if (sizeof($result) > 0) { + + foreach ($result as $doc) { + if ($doc->getType() === ItemDocument::TYPE_TEST && $doc->getId() === 1) { + $output->writeln('<info>ok</info>'); + return true; + } + } + + // CHECK ID DOCUMENT + $output->writeln('<error>fail</error>'); + return false; + } + + $output->writeln('<error>fail</error>'); + return false; + } + + $output->writeln('<error>fail</error> - ' . $ierror->getCode()); + return false; + } + + private function test_delete($output) + { + $output->write(' - Deleting test document: '); + + $doc = new ItemDocument(ItemDocument::TYPE_TEST, 1); + $data = array( + $doc + ); + $this->indexService->removeDocuments($data, $ierror); + if ($doc->isRemoved()) { + $output->writeln('<info>ok</info>'); + return true; + } + + $output->writeln('<error>fail</error> - ' . $ierror->getCode()); + return false; + } +} + + + |