|
| 1 | +<?php |
| 2 | +declare(strict_types=1); |
| 3 | + |
| 4 | +namespace Flowpack\ElasticSearch\ContentRepositoryAdaptor\Tests\Functional\Indexer; |
| 5 | + |
| 6 | +/* |
| 7 | + * This file is part of the Flowpack.ElasticSearch.ContentRepositoryAdaptor package. |
| 8 | + * |
| 9 | + * (c) Contributors of the Neos Project - www.neos.io |
| 10 | + * |
| 11 | + * This package is Open Source Software. For the full copyright and license |
| 12 | + * information, please view the LICENSE file which was distributed with this |
| 13 | + * source code. |
| 14 | + */ |
| 15 | + |
| 16 | +use Flowpack\ElasticSearch\ContentRepositoryAdaptor\ElasticSearchClient; |
| 17 | +use Flowpack\ElasticSearch\ContentRepositoryAdaptor\Exception\ConfigurationException; |
| 18 | +use Flowpack\ElasticSearch\ContentRepositoryAdaptor\Indexer\NodeIndexer; |
| 19 | +use Flowpack\ElasticSearch\ContentRepositoryAdaptor\Service\DimensionsService; |
| 20 | +use Flowpack\ElasticSearch\Exception; |
| 21 | +use Flowpack\ElasticSearch\Transfer\Exception\ApiException; |
| 22 | +use Neos\Flow\Tests\FunctionalTestCase; |
| 23 | + |
| 24 | +class NodeIndexerTest extends FunctionalTestCase |
| 25 | +{ |
| 26 | + const TESTING_INDEX_PREFIX = 'neoscr_testing'; |
| 27 | + |
| 28 | + /** |
| 29 | + * @var NodeIndexer |
| 30 | + */ |
| 31 | + protected $nodeIndexer; |
| 32 | + |
| 33 | + /** |
| 34 | + * @var DimensionsService |
| 35 | + */ |
| 36 | + protected $dimensionService; |
| 37 | + |
| 38 | + /** |
| 39 | + * @var ElasticSearchClient |
| 40 | + */ |
| 41 | + protected $searchClient; |
| 42 | + |
| 43 | + public function setUp(): void |
| 44 | + { |
| 45 | + parent::setUp(); |
| 46 | + $this->searchClient = $this->objectManager->get(ElasticSearchClient::class); |
| 47 | + $this->nodeIndexer = $this->objectManager->get(NodeIndexer::class); |
| 48 | + $this->dimensionService = $this->objectManager->get(DimensionsService::class); |
| 49 | + } |
| 50 | + |
| 51 | + protected function tearDown(): void |
| 52 | + { |
| 53 | + parent::tearDown(); |
| 54 | + $this->searchClient->request('DELETE', '/' . self::TESTING_INDEX_PREFIX . '*'); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * @test |
| 59 | + */ |
| 60 | + public function getIndexWithoutDimensionConfigured(): void |
| 61 | + { |
| 62 | + $this->nodeIndexer->setDimensions([]); |
| 63 | + $index = $this->nodeIndexer->getIndex(); |
| 64 | + static::assertEquals(self::TESTING_INDEX_PREFIX . '-default', $index->getName()); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * @test |
| 69 | + * |
| 70 | + * @throws \Flowpack\ElasticSearch\ContentRepositoryAdaptor\Exception |
| 71 | + * @throws ConfigurationException |
| 72 | + * @throws Exception |
| 73 | + */ |
| 74 | + public function getIndexForDimensionConfiguration(): void |
| 75 | + { |
| 76 | + $dimensionValues = ['language' => ['de']]; |
| 77 | + $this->nodeIndexer->setDimensions($dimensionValues); |
| 78 | + $index = $this->nodeIndexer->getIndex(); |
| 79 | + $dimesionHash = $this->dimensionService->hash($dimensionValues); |
| 80 | + |
| 81 | + static::assertEquals(self::TESTING_INDEX_PREFIX . '-' . $dimesionHash, $index->getName()); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * @test |
| 86 | + */ |
| 87 | + public function updateIndexAlias(): void |
| 88 | + { |
| 89 | + $dimensionValues = ['language' => ['de']]; |
| 90 | + $this->nodeIndexer->setDimensions($dimensionValues); |
| 91 | + $this->nodeIndexer->setIndexNamePostfix((string)time()); |
| 92 | + $this->nodeIndexer->getIndex()->create(); |
| 93 | + |
| 94 | + $this->assertIndexExists($this->nodeIndexer->getIndexName()); |
| 95 | + $this->nodeIndexer->updateIndexAlias(); |
| 96 | + |
| 97 | + $this->assertAliasesEquals(self::TESTING_INDEX_PREFIX, [$this->nodeIndexer->getIndexName()]); |
| 98 | + } |
| 99 | + |
| 100 | + |
| 101 | + /** |
| 102 | + * @param string $indexName |
| 103 | + * @throws \Flowpack\ElasticSearch\Transfer\Exception |
| 104 | + * @throws ApiException |
| 105 | + * @throws \Neos\Flow\Http\Exception |
| 106 | + */ |
| 107 | + protected function assertIndexExists(string $indexName): void |
| 108 | + { |
| 109 | + $response = $this->searchClient->request('HEAD', '/' . $indexName); |
| 110 | + self::assertEquals(200, $response->getStatusCode()); |
| 111 | + } |
| 112 | + |
| 113 | + protected function assertAliasesEquals(string $aliasPrefix, array $expectdAliases): void |
| 114 | + { |
| 115 | + $content = $this->searchClient->request('GET', '/_alias/' . $aliasPrefix . '*')->getTreatedContent(); |
| 116 | + static::assertEquals($expectdAliases, array_keys($content)); |
| 117 | + } |
| 118 | +} |
0 commit comments