|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the TYPO3 CMS project. |
| 5 | + * |
| 6 | + * It is free software; you can redistribute it and/or modify it under |
| 7 | + * the terms of the GNU General Public License, either version 2 |
| 8 | + * of the License, or any later version. |
| 9 | + * |
| 10 | + * For the full copyright and license information, please read the |
| 11 | + * LICENSE.txt file that was distributed with this source code. |
| 12 | + * |
| 13 | + * The TYPO3 project - inspiring people to share! |
| 14 | + */ |
| 15 | + |
| 16 | +namespace ApacheSolrForTypo3\Solr\Tests\Integration\Extbase; |
| 17 | + |
| 18 | +use ApacheSolrForTypo3\FakeExtension\Domain\Model\Foo; |
| 19 | +use ApacheSolrForTypo3\FakeExtension\Domain\Repository\FooRepository; |
| 20 | +use ApacheSolrForTypo3\Solr\IndexQueue\Queue; |
| 21 | +use ApacheSolrForTypo3\Solr\Tests\Integration\IntegrationTest; |
| 22 | +use TYPO3\CMS\Core\Context\Context; |
| 23 | +use TYPO3\CMS\Core\Utility\GeneralUtility; |
| 24 | +use TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager; |
| 25 | + |
| 26 | +class PersistenceEventListenerTest extends IntegrationTest |
| 27 | +{ |
| 28 | + protected $testExtensionsToLoad = [ |
| 29 | + 'typo3conf/ext/solr', |
| 30 | + '../vendor/apache-solr-for-typo3/solr/Tests/Integration/Fixtures/Extensions/fake_extension', |
| 31 | + ]; |
| 32 | + |
| 33 | + protected ?Queue $indexQueue = null; |
| 34 | + protected ?FooRepository $repository = null; |
| 35 | + protected ?PersistenceManager $persistenceManager = null; |
| 36 | + |
| 37 | + protected function setUp(): void |
| 38 | + { |
| 39 | + parent::setUp(); |
| 40 | + $this->writeDefaultSolrTestSiteConfiguration(); |
| 41 | + $this->addTypoScriptToTemplateRecord(1, ' |
| 42 | + plugin.tx_solr.index.queue.foo = 1 |
| 43 | + plugin.tx_solr.index.queue.foo.type = tx_fakeextension_domain_model_foo |
| 44 | + '); |
| 45 | + $this->indexQueue = GeneralUtility::makeInstance(Queue::class); |
| 46 | + $this->repository = GeneralUtility::makeInstance(FooRepository::class); |
| 47 | + $this->persistenceManager = GeneralUtility::makeInstance(PersistenceManager::class); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * @test |
| 52 | + */ |
| 53 | + public function newEntityIsAddedToIndexQueue() |
| 54 | + { |
| 55 | + $object = new Foo(); |
| 56 | + $object->setTitle('Added'); |
| 57 | + $object->setPid(1); |
| 58 | + $repository = GeneralUtility::makeInstance(FooRepository::class); |
| 59 | + $repository->add($object); |
| 60 | + $this->persistenceManager->persistAll(); |
| 61 | + self::assertTrue($this->indexQueue->containsItem('tx_fakeextension_domain_model_foo', 1)); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * @test |
| 66 | + */ |
| 67 | + public function newHiddenEntityIsNotAddedToIndexQueue() |
| 68 | + { |
| 69 | + $object = new Foo(); |
| 70 | + $object->setTitle('Added'); |
| 71 | + $object->setHidden(true); |
| 72 | + $object->setPid(1); |
| 73 | + $repository = GeneralUtility::makeInstance(FooRepository::class); |
| 74 | + $repository->add($object); |
| 75 | + $this->persistenceManager->persistAll(); |
| 76 | + self::assertFalse($this->indexQueue->containsItem('tx_fakeextension_domain_model_foo', 1)); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * @test |
| 81 | + */ |
| 82 | + public function updatedEntityIsUpdatedInIndexQueue() |
| 83 | + { |
| 84 | + $this->importCSVDataSet(__DIR__ . '/Fixtures/update_items.csv'); |
| 85 | + /** @var Foo $object */ |
| 86 | + $object = $this->repository->findByUid(2); |
| 87 | + $object->setTitle('Updated'); |
| 88 | + $this->repository->update($object); |
| 89 | + $this->persistenceManager->persistAll(); |
| 90 | + |
| 91 | + $context = GeneralUtility::makeInstance(Context::class); |
| 92 | + $currentTimestamp = $context->getPropertyFromAspect('date', 'timestamp'); |
| 93 | + |
| 94 | + self::assertTrue($this->indexQueue->containsItem('tx_fakeextension_domain_model_foo', 2)); |
| 95 | + |
| 96 | + $item = $this->indexQueue->getItem(1); |
| 97 | + self::assertSame(2, $item->getRecordUid()); |
| 98 | + self::assertSame('foo', $item->getIndexingConfigurationName()); |
| 99 | + self::assertSame($currentTimestamp, $item->getChanged()); |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * @test |
| 104 | + */ |
| 105 | + public function softDeletedEntityIsRemovedFromIndexQueue() |
| 106 | + { |
| 107 | + $this->importCSVDataSet(__DIR__ . '/Fixtures/delete_items.csv'); |
| 108 | + /** @var Foo $object */ |
| 109 | + $object = $this->repository->findByUid(3); |
| 110 | + $this->repository->remove($object); |
| 111 | + $this->persistenceManager->persistAll(); |
| 112 | + |
| 113 | + self::assertFalse($this->indexQueue->containsItem('tx_fakeextension_domain_model_foo', 3)); |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * @test |
| 118 | + */ |
| 119 | + public function deletedEntityIsRemovedFromIndexQueue() |
| 120 | + { |
| 121 | + unset($GLOBALS['TCA']['tx_fakeextension_domain_model_foo']['ctrl']['delete']); |
| 122 | + $this->importCSVDataSet(__DIR__ . '/Fixtures/delete_items.csv'); |
| 123 | + /** @var Foo $object */ |
| 124 | + $object = $this->repository->findByUid(3); |
| 125 | + $this->repository->remove($object); |
| 126 | + $this->persistenceManager->persistAll(); |
| 127 | + |
| 128 | + self::assertFalse($this->indexQueue->containsItem('tx_fakeextension_domain_model_foo', 3)); |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * @test |
| 133 | + */ |
| 134 | + public function updatedEntityTurnedHiddenIsRemovedFromIndexQueue() |
| 135 | + { |
| 136 | + $this->importCSVDataSet(__DIR__ . '/Fixtures/hidden_items.csv'); |
| 137 | + /** @var Foo $object */ |
| 138 | + $object = $this->repository->findByUid(4); |
| 139 | + $object->setHidden(true); |
| 140 | + $this->repository->update($object); |
| 141 | + $this->persistenceManager->persistAll(); |
| 142 | + |
| 143 | + self::assertFalse($this->indexQueue->containsItem('tx_fakeextension_domain_model_foo', 4)); |
| 144 | + } |
| 145 | +} |
0 commit comments