diff --git a/modules/search_api_db/tests/src/Kernel/BackendTest.php b/modules/search_api_db/tests/src/Kernel/BackendTest.php index 1fad980..9309044 100644 --- a/modules/search_api_db/tests/src/Kernel/BackendTest.php +++ b/modules/search_api_db/tests/src/Kernel/BackendTest.php @@ -122,7 +122,7 @@ class BackendTest extends BackendTestBase { */ protected function updateIndex() { /** @var \Drupal\search_api\IndexInterface $index */ - $index = $this->getIndex(); + $index = $this->getEditableIndex(); // Remove a field from the index and check if the change is matched in the // server configuration. @@ -197,7 +197,7 @@ class BackendTest extends BackendTestBase { * (optional) Whether partial matching should be enabled or disabled. */ protected function editServerPartial($enable = TRUE) { - $server = $this->getServer(); + $server = $this->getEditableServer(); $backend_config = $server->getBackendConfig(); $backend_config['partial_matches'] = $enable; $server->setBackendConfig($backend_config); @@ -260,7 +260,7 @@ class BackendTest extends BackendTestBase { * Edits the server to change the "Minimum word length" setting. */ protected function editServerMinChars() { - $server = $this->getServer(); + $server = $this->getEditableServer(); $backend_config = $server->getBackendConfig(); $backend_config['min_chars'] = 4; $backend_config['partial_matches'] = FALSE; @@ -386,13 +386,13 @@ class BackendTest extends BackendTestBase { * Checks that field ID changes are treated correctly (without re-indexing). */ protected function checkFieldIdChanges() { - $this->getIndex() + $this->getEditableIndex() ->renameField('type', 'foobar') ->save(); $results = $this->buildSearch(NULL, array('foobar,item'))->execute(); $this->assertResults(array(1, 2, 3), $results, 'Search after renaming a field.'); - $this->getIndex()->renameField('foobar', 'type')->save(); + $this->getEditableIndex()->renameField('foobar', 'type')->save(); } /** @@ -489,8 +489,8 @@ class BackendTest extends BackendTestBase { // See whether clearing the server works. // Regression test for #2156151. - $server = $this->getServer(); - $index = $this->getIndex(); + $server = $this->getEditableServer(); + $index = $this->getEditableIndex(); $server->deleteAllIndexItems($index); $query = $this->buildSearch(); $results = $query->execute(); diff --git a/src/TemporaryConfOverrideLayer/OverrideSupportingConfigEntityBase.php b/src/TemporaryConfOverrideLayer/OverrideSupportingConfigEntityBase.php index af18edd..2140a3d 100644 --- a/src/TemporaryConfOverrideLayer/OverrideSupportingConfigEntityBase.php +++ b/src/TemporaryConfOverrideLayer/OverrideSupportingConfigEntityBase.php @@ -31,6 +31,19 @@ abstract class OverrideSupportingConfigEntityBase extends ConfigEntityBase imple /** * {@inheritdoc} */ + public function getEditableEntity() { + if ($this->isNew() || !$this->isImmutable()) { + return $this; + } + + /** @var \Drupal\Core\Config\Entity\ConfigEntityStorageInterface $storage */ + $storage = $this->entityTypeManager()->getStorage($this->entityTypeId); + return $storage->loadOverrideFree($this->id()); + } + + /** + * {@inheritdoc} + */ public function save() { // Ensure an immutable configuration entity is not saved if the entity type // supports config overrides. diff --git a/src/TemporaryConfOverrideLayer/OverrideSupportingConfigEntityInterface.php b/src/TemporaryConfOverrideLayer/OverrideSupportingConfigEntityInterface.php index 0fd1239..4412a46 100644 --- a/src/TemporaryConfOverrideLayer/OverrideSupportingConfigEntityInterface.php +++ b/src/TemporaryConfOverrideLayer/OverrideSupportingConfigEntityInterface.php @@ -23,4 +23,13 @@ interface OverrideSupportingConfigEntityInterface extends ConfigEntityInterface */ public function isImmutable(); + /** + * Returns an editable version of this config entity. + * + * May return the same instance if this instance is not immutable. + * + * @return \Drupal\search_api\TemporaryConfOverrideLayer\OverrideSupportingConfigEntityInterface + */ + public function getEditableEntity(); + } diff --git a/src/UnsavedIndexConfiguration.php b/src/UnsavedIndexConfiguration.php index 4e2a803..37adbda 100644 --- a/src/UnsavedIndexConfiguration.php +++ b/src/UnsavedIndexConfiguration.php @@ -1064,4 +1064,17 @@ class UnsavedIndexConfiguration implements IndexInterface, UnsavedConfigurationI return $this->entity->getThirdPartyProviders(); } + /** + * {@inheritdoc} + */ + public function isImmutable() { + return FALSE; + } + + /** + * {@inheritdoc} + */ + public function getEditableEntity() { + return $this; + } } diff --git a/tests/src/Kernel/BackendTestBase.php b/tests/src/Kernel/BackendTestBase.php index adb91c9..ab956ba 100644 --- a/tests/src/Kernel/BackendTestBase.php +++ b/tests/src/Kernel/BackendTestBase.php @@ -178,7 +178,7 @@ abstract class BackendTestBase extends KernelTestBase { * Enables the "HTML Filter" processor for the index. */ protected function enableHtmlFilter() { - $index = $this->getIndex(); + $index = $this->getEditableIndex(); /** @var \Drupal\search_api\Processor\ProcessorInterface $processor */ $processor = $index->createPlugin('processor', 'html_filter'); @@ -191,7 +191,7 @@ abstract class BackendTestBase extends KernelTestBase { * Disables the "HTML Filter" processor for the index. */ protected function disableHtmlFilter() { - $index = $this->getIndex(); + $index = $this->getEditableIndex(); $index->removeField('body'); $index->removeProcessor('html_filter'); $index->save(); @@ -844,7 +844,7 @@ abstract class BackendTestBase extends KernelTestBase { * @see https://www.drupal.org/node/1916474 */ protected function regressionTest1916474() { - $index = $this->getIndex(); + $index = $this->getEditableIndex(); $this->addField($index, 'prices', 'decimal'); $success = $index->save(); $this->assertTrue($success, 'The index field settings were successfully changed.'); @@ -900,7 +900,7 @@ abstract class BackendTestBase extends KernelTestBase { * @see https://www.drupal.org/node/2616268 */ protected function regressionTest2471509() { - $index = $this->getIndex(); + $index = $this->getEditableIndex(); $this->addField($index, 'body'); $index->save(); $this->indexItems($this->indexId); @@ -913,7 +913,7 @@ abstract class BackendTestBase extends KernelTestBase { $count = $this->indexItems($this->indexId); $this->assertEquals(1, $count, 'Indexing an item with a word longer than 50 characters worked.'); - $index = $this->getIndex(); + $index = $this->getEditableIndex(); $index->getField('body')->setType('string'); $index->save(); $count = $this->indexItems($this->indexId); @@ -959,10 +959,10 @@ abstract class BackendTestBase extends KernelTestBase { // Test the same body when indexed as a string (255 characters limit should // not be reached). - $index = $this->getIndex(); + $index = $this->getEditableIndex(); $index->getField('body')->setType('string'); $index->save(); - $count = $index->indexItems(); + $count = $this->indexItems($this->indexId); $this->assertEquals($entity_count, $count, 'Switching type from text to string worked.'); $query = $this->buildSearch(NULL, array("body,$mb_body")); @@ -1047,6 +1047,17 @@ abstract class BackendTestBase extends KernelTestBase { } /** + * Retrieves the search server used by this test in an editable state. + * + * @return \Drupal\search_api\ServerInterface + * The search server. + */ + protected function getEditableServer() { + return $this->getServer() + ->getEditableEntity(); + } + + /** * Retrieves the search index used by this test. * * @return \Drupal\search_api\IndexInterface @@ -1057,6 +1068,17 @@ abstract class BackendTestBase extends KernelTestBase { } /** + * Retrieves the search index used by this test in an editable state. + * + * @return \Drupal\search_api\IndexInterface + * The search index. + */ + protected function getEditableIndex() { + return $this->getIndex() + ->getEditableEntity(); + } + + /** * Adds a field to a search index. * * The index will not be saved automatically.