Unverified Commit aa038bbc authored by Alex Pott's avatar Alex Pott
Browse files

fix: #3582171 [PHP 8.5] Strengthen views data with entity types w/o data tables

By: claudiu.cristea
By: alexpott
(cherry picked from commit 4b063c5b93f0359a192505fb8cf0be1e7616add1)
parent 0ecadb79
Loading
Loading
Loading
Loading
Loading
+18 −16
Original line number Diff line number Diff line
@@ -173,6 +173,7 @@ public function defaultFieldImplementation(FieldStorageConfigInterface $field_st
      ];
    }

    if ($data_table) {
      if ($translation_join_type === 'language_bundle') {
        $data[$table_alias]['table']['join'][$data_table]['join_id'] = 'field_or_language_join';
        $data[$table_alias]['table']['join'][$data_table]['extra'][] = [
@@ -184,12 +185,13 @@ public function defaultFieldImplementation(FieldStorageConfigInterface $field_st
          'value' => $untranslatable_config_bundles,
        ];
      }
    elseif ($translation_join_type === 'language' && $data_table) {
      elseif ($translation_join_type === 'language') {
        $data[$table_alias]['table']['join'][$data_table]['extra'][] = [
          'left_field' => 'langcode',
          'field' => 'langcode',
        ];
      }
    }

    if ($supports_revisions) {
      $table_alias = $field_tables[EntityStorageInterface::FIELD_LOAD_REVISION]['alias'];
+88 −0
Original line number Diff line number Diff line
<?php

declare(strict_types=1);

namespace Drupal\Tests\views\Kernel;

use Drupal\entity_test\EntityTestHelper;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\KernelTests\KernelTestBase;
use Drupal\views\FieldViewsDataProvider;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\RunTestsInSeparateProcesses;

/**
 * Tests field views data in an edge case scenario.
 *
 * Tests the field views data case when:
 * - The entity type doesn't have a data table.
 * - A configurable field storage is translatable.
 * - It has at least two bundles exposing the field with different
 *   translatability settings.
 */
#[CoversClass(FieldViewsDataProvider::class)]
#[Group('views')]
#[RunTestsInSeparateProcesses]
class EntityWithoutBaseTableTest extends KernelTestBase {

  /**
   * {@inheritdoc}
   */
  protected static $modules = [
    'field',
    'entity_test',
    'user',
    'views',
    'system',
  ];

  /**
   * {@inheritdoc}
   */
  protected function setUp(): void {
    parent::setUp();

    // This entity type doesn't have a data table.
    $this->installEntitySchema('entity_test_label');

    EntityTestHelper::createBundle(bundle: 'bundle_with_translatable_field', entity_type: 'entity_test_label');
    EntityTestHelper::createBundle(bundle: 'bundle_with_untranslatable_field', entity_type: 'entity_test_label');

    FieldStorageConfig::create([
      'entity_type' => 'entity_test_label',
      'type' => 'string',
      'field_name' => 'string_field',
      'translatable' => TRUE,
    ])->save();
    // This field instance is translatable.
    FieldConfig::create([
      'entity_type' => 'entity_test_label',
      'bundle' => 'bundle_with_translatable_field',
      'field_name' => 'string_field',
      'translatable' => TRUE,
    ])->save();
    // This field instance is not translatable.
    FieldConfig::create([
      'entity_type' => 'entity_test_label',
      'bundle' => 'bundle_with_untranslatable_field',
      'field_name' => 'string_field',
      'translatable' => FALSE,
    ])->save();
  }

  /**
   * Tests that the entity without a data table doesn't emit deprecation notice.
   *
   * @legacy-covers ::defaultFieldImplementation
   */
  public function testEntityWithoutDataTable(): void {
    $entity_type = $this->container->get('entity_type.manager')->getDefinition('entity_test_label');
    $this->assertNull($entity_type->getDataTable());
    $this->assertNotNull($entity_type->getBaseTable());
    $views_data = $this->container->get('views.views_data')->getAll();
    $this->assertArrayHasKey($entity_type->getBaseTable(), $views_data);
  }

}