cakephp/cakephp

Tests have wrong Connection aliases if testing with fixtures with different connection name

calcosta opened this issue · 5 comments

Description

I'm upgrading my application from Cake4 to Cake5 and noticed, that my tests fail if fixtures with different connection names are loaded (e.g. from a plugin).
I tried to narrow down the problem and it seems, that the function addTestAliases() in file src/TestSuite/ConnectionHelper.php is causing the problem.

My Fixture class looks like this:

class InvoicesFixture extends TestFixture
{
    /**
     * Test connection
     *
     * @var string
     */
    public string $connection = 'test_myapp';

    public array $records = [

The aliases created by this function look like that:

[
  'debug_kit' => 'test_debug_kit',
  'default' => 'test',
  'test_myapp' => 'myapp', // this is wrong
  'myapp' => 'test_myapp',
  'test_debug_kit' => 'debug_kit'
]

The code in ConnectionHelper.php

public function addTestAliases(): void
{
[...]
else {
    $test = 'test_' . $connection;
    ConnectionManager::alias($connection, $test);
}

should be in my opinion

public function addTestAliases(): void
{
[...]
else {
    $test = 'test_' . $connection;
    ConnectionManager::alias($test, $connection);
}

Currently running the tests truncates my development tables 😕 and tests fail because the data is inserted into the wrong database.

CakePHP Version

5.0.6

PHP Version

8.2

Are you using the Fixture Factories plugin? We've had some trouble with aliases when using those.

Could this be the underlying issue causing vierge-noire/cakephp-fixture-factories#187 and/or vierge-noire/cakephp-fixture-factories#233?

Are you using the Fixture Factories plugin? We've had some trouble with aliases when using those.

No, I'm not using the plugin.

Does your application configuration, or tests/bootstrap.php or application bootstrapping define the connection for test_myapp?

This was recently changed in #17261 and your suggestion would regress that fix. Perhaps it was the elasticsearch plugin that was incorrect?

Does your application configuration, or tests/bootstrap.php or application bootstrapping define the connection for test_myapp?

The configuration for test_myapp database is done in config/app_local.php in the main app folder (not the plugin).

If i get this right, the current code always creates an alias for non default connections in both directions, right? That doesn't make sense, does it?

If i get this right, the current code always creates an alias for non default connections in both directions, right? That doesn't make sense, does it?

Yeah looking at the aliasing more, it doesn't entirely make sense. I'll have to look into what is going on with the elasticsearch plugin that fueled the previous change.