Problem/Motivation

If you use index prefix/suffix in search-api server configuration, you cannot access the "search-api index" name in the IndexEvent.

It only contain "real index name" (prefixed/suffixed) and index data.

If we want to add some element to the index query which need a verification on the index configuration (like adding an ingest pipeline), we cannot retrieve the index "search-api machine name".

Proposed resolution

Two way can be discuss:

  1. adding the "original Id" of the index only in the IndexParamsEvent.
  2. adding the "original Id" of the index in all Event - let's do that in future tickets, see #3

Remaining tasks

  1. discuss about the better solution - done by @drdam and @mparker17 by #3
  2. Merge request for 8.0.x - merge request !101 created from the code by @drdam in #4
  3. Add tests - no need, it already has them, see #11
  4. Community review - skipped by @mparker17 in #11
  5. Maintainer review - done by @mparker17 in #11
  6. Merge request for 9.0.x - merge request !197 created by @mparker17 in #14
  7. Commit to 9.0.x - done by @mparker17 in #16
  8. Commit to 8.0.x - done by @mparker17 in #18
  9. Release 9.0.x - released in 9.0.0-alpha3 by @mparker17
  10. Release 8.0.x - released in 8.0.0-alpha7 by @mparker17

User interface changes

None.

API changes

Adds the index's "original id" to the IndexParamsEvent.

Data model changes

None.

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

drdam created an issue. See original summary.

drdam’s picture

Title: Add OriginalIndexId in IndexParamsEnvent » Add OriginalIndexId in IndexParamsEvent
mparker17’s picture

Issue summary: View changes

@drdam, thank you very much!


  1. adding the "original Id" of the index only in the IndexParamsEvent.
  2. adding the "original Id" of the index in all Event

I think for now, let's add the "original Id" of the index only in the IndexParamsEvent; and work on the other events in future issues, because it will be easier for me to review and merge smaller changes. Also, once we have figured out all the work that we need to do for this ticket, the future tickets will be easier/faster.

mparker17’s picture

Title: Add OriginalIndexId in IndexParamsEvent » Add OriginalIndexId to IndexParamsEvent
Issue summary: View changes

I've created a merge request with the changes; let's see what testbot thinks.

mparker17’s picture

Status: Active » Needs review

Moving the issue to Needs Review.

mparker17’s picture

Issue tags: +Needs tests

Updating issue tags

mparker17’s picture

Status: Needs review » Needs work

Briefly checking the status of this issue after releasing 8.0.0-alpha5...

  1. The existing tests are failing, indicating a change in behavior. Essentially, there are two paths forward: update the tests, or fix the regression. I apologize that I haven't ported this documentation to Elasticsearch Connector yet, but I wrote some documentation for the Sitemap module that explains this in more detail.
  2. The new code path also doesn't have any tests, and it will need a test before I can merge it.
  3. There are some phpcs lints to fix
drdam’s picture

I have find the bug, but now I have an error I don't understand :

1) Drupal\Tests\elasticsearch_connector\Unit\SearchAPI\IndexParamBuilderTest::testbuildIndexParams
Prophecy\Exception\Call\UnexpectedCallException: Unexpected method call on Double\IndexInterface\P8:
- getOriginalId(

)
expected calls were:
- id(

)

/var/www/html/vendor/phpspec/prophecy/src/Prophecy/Call/CallCenter.php:203
/var/www/html/vendor/phpspec/prophecy/src/Prophecy/Call/CallCenter.php:160
/var/www/html/vendor/phpspec/prophecy/src/Prophecy/Prophecy/ObjectProphecy.php:224
/var/www/html/vendor/phpspec/prophecy/src/Prophecy/Prophet.php:137
/var/www/html/vendor/phpspec/prophecy-phpunit/src/ProphecyTrait.php:73
/var/www/html/vendor/bin/phpunit:122

mparker17’s picture

@drdam, I apologize for the time it has taken me to respond to your message!

The error you were seeing...

Prophecy\Exception\Call\UnexpectedCallException: Unexpected method call on Double\IndexInterface\P8:
- getOriginalId(

)
expected calls were:
- id(

)

... is related to test doubles (a technique used for writing unit tests)...

In this case, the error is telling us that we need to update an \Drupal\search_api\IndexInterface test-double that we are using in the test (\Drupal\Tests\elasticsearch_connector\Unit\SearchAPI\IndexParamBuilderTest::testbuildIndexParams()) to expect a new call to a method named getOriginalId() - we added this call in the production code when we create an IndexParamsEvent in \Drupal\elasticsearch_connector\SearchAPI\IndexParamBuilder::buildIndexParams().

In this case, to tell the test-double what to do when getOriginalId() gets called on it, we would change it like this...

diff --git a/tests/src/Unit/SearchAPI/IndexParamBuilderTest.php b/tests/src/Unit/SearchAPI/IndexParamBuilderTest.php
index fe7e5e3..e409234 100644
--- a/tests/src/Unit/SearchAPI/IndexParamBuilderTest.php
+++ b/tests/src/Unit/SearchAPI/IndexParamBuilderTest.php
@@ -54,6 +54,7 @@ public function testbuildIndexParams() {
     $index = $this->prophesize(IndexInterface::class);
     $indexId = "index_" . $this->randomMachineName();
     $index->id()->willReturn($indexId);
+    $index->getOriginalId()->willReturn($indexId);
 
     $field1Id = "field1_" . $this->randomMachineName(8);
     $field2Id = "field2_" . $this->randomMachineName(8);

Aside:

"Test doubles" are sometimes called "substitutions", and they're simple substitute objects that you can use in place of something more complicated, and there are bunch of related terms: "mock objects"/"mocks", "dummy objects"/"dummies", "fakes", "stubs", "spies", etc. Basically, we want unit tests to be as decoupled (and fast to write and run) as possible, so we use test doubles so our unit test can run in isolation, regardless of how those other systems change. We tell the test doubles what to expect, and they alert us (i.e.: with an error like what you saw) when the expectations change. When we get an error like that, we have to decide if that change was accidental (i.e.: fix our production code) or intentional (i.e.: fix our tests) - in this case, it was intentional, so we fixed our tests by telling the test double to expect something new.

If you want to find out more about test doubles, I recommend reading:

  1. the "Test Doubles" section in the PHPUnit manual,
  2. this blog post on Testing With and Without Dependencies by the PHPUnit creator,
  3. this video on Test Driven Development (although note the video is about the Rust programming language), and
  4. the README for the library we're using to create test doubles in this test, Prophecy

I'm hoping to merge this code before a beta release, so I'm going to work on this issue a bit (I will probably write more tests). I'll post here when I'm done, and will likely request that you test my changes. Thank you in advance for your patience with me!

mparker17’s picture

Version: 8.0.x-dev » 9.0.x-dev
Status: Needs work » Reviewed & tested by the community
Issue tags: -Needs tests +Needs change record

Actually, it turns out that IndexParamsEvent already has a functional test in \Drupal\elasticsearch_connector_test\EventSubscriber\ElasticSearchEventSubscribers::onIndexParams(); and we know the new functionality is already tested because of the test-double error that we fixed earlier.

I'll write a change record, create a 9.0.x branch, then merge it.

Thank you very much for your contribution, @drdam!

mparker17’s picture

Issue summary: View changes
mparker17’s picture

I've added a change record: IndexParamsEvent now has a getOriginalIndexId() method

I will now create a merge request for the 9.0.x branch. I am tagging this issue with "Needs backport to 8.0.x" so I remember to merge the original MR.

mparker17’s picture

Issue summary: View changes

  • mparker17 committed f2fd666b on 9.0.x
    feat: #3542571 Add OriginalIndexId to IndexParamsEvent
    
    By: drdam
    
mparker17’s picture

Version: 9.0.x-dev » 8.0.x-dev
Issue summary: View changes
Status: Reviewed & tested by the community » Patch (to be ported)
Issue tags: -needs backport to 8.0.x

Merged to 9.0.x; will merge to 8.0.x next.

  • mparker17 committed 90344470 on 8.0.x
    feat: #3542571 Add OriginalIndexId to IndexParamsEvent
    
    By: drdam
    
mparker17’s picture

Issue summary: View changes
Status: Patch (to be ported) » Fixed
Issue tags: -change record added

Merged to 8.0.x, and published the change record.

I will update this issue when I make a release.

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

mparker17’s picture

Issue summary: View changes

The changes in this issue were released in elasticsearch_connector-9.0.0-alpha3, and elasticsearch_connector-8.0.0-alpha7

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.